﻿using IM.Easy.Core.Attributes;
using IM.Easy.Core.Result;
using IM.Easy.Core.Utils;
using IM.Easy.CoreServices.Dto;
using IM.Easy.CoreServices.IServices;
using IM.Easy.Infrastructure.Query;
using IM.Easy.SystemServices.Dtos;
using IM.Easy.SystemServices.IServices;
using Microsoft.AspNetCore.Mvc;

namespace IM.Easy.Api.Controllers.System
{
    /// <summary>
    /// 字典类型服务
    /// </summary>
    [Route("system/dict/group")]
    public class DictGroupController : ControllerBase
    {
        private readonly IDictTypeService _dictTypeService;
        private readonly IDictDataService _dictDataService;
        private readonly IDictGlobalService _dictGlobalService;

        public DictGroupController(IDictTypeService dictTypeService, IDictDataService dictDataService, IDictGlobalService dictGlobalService)
        {
            _dictTypeService = dictTypeService;
            _dictDataService = dictDataService;
            _dictGlobalService = dictGlobalService;
        }

        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        [Api(Name = "获取字典组列表")]
        public PageResponse<DictTypeDto> GetList([FromQuery] DictTypeQueryDto query)
        {
            var list = _dictTypeService.GetList(query);
            return list;
        }

        /// <summary>
        /// 获取下拉选项列表
        /// </summary>
        /// <returns></returns>
        [Api(Name = "字典组下拉列表", AllowAnonymous = true)]
        public List<DictTypeDto> GetOptionList([FromQuery] DictTypeQueryDto query)
        {
            query.PageIndex = 0;
            query.PageSize = int.MaxValue;
            var page = _dictTypeService.GetList(query);
            var list = page.Result;
            return list;
        }

        /// <summary>
        /// 添加字典类型
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        [Api(Name = "添加字典组", Perm = "dict:group:add")]
        public async Task<long> Add([FromBody] DictTypeDto dto)
        {
            var id = await _dictTypeService.AddAsync(dto);
            return id;
        }

        /// <summary>
        /// 更新字典类型
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [Api(Name = "更新字典组", Perm = "dict:group:put")]
        public bool Update([FromBody] DictTypeDto dto)
        {
            return _dictTypeService.Update(dto);
        }

        /// <summary>
        /// 通过key获取字典详情
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        [HttpGet("{id}")]
        [Api(Name = "获取字典组明细")]
        public DictTypeDto Get([FromRoute] long id)
        {
            return _dictTypeService.GetById(id);
        }

        /// <summary>
        /// 删除----支持批量
        /// </summary>
        /// <param name="ids"></param>
        [HttpDelete("bulk")]
        [Api(AllowAnonymous = false, Desc = "批量删除字典组", Name = "批量删除字典组", Perm = "dict:group:bulk:del")]
        public BlukResult BulkDelete([FromBody] params long[] ids)
        {
            var result = new BlukResult();
            foreach (var id in ids)
            {
                var hasData = _dictTypeService.HasDictData(id);
                if (hasData)
                    result.AddFailure(id, $"系统编码为{id}下存在字典数据");
                else
                {
                    var flag = _dictTypeService.DeleteById(id);
                    if (flag) result.AddSuccess();
                    else result.AddFailure(id, $"删除失败");
                }
            }
            return result;
        }

        /// <summary>
        /// 单个删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpDelete("{id}")]
        [Api(AllowAnonymous = false, Desc = "单个删除字典组", Name = "删除字典组", Perm = "dict:group:del")]
        public bool Delete([FromRoute] long id)
        {
            var hasData = _dictTypeService.HasDictData(id);
            if (hasData)
            {
                ExceptionUtils.ThrowBusinessException($"系统编码为{id}下存在字典数据");
                return false;
            }
            else
            {
                var flag = _dictTypeService.DeleteById(id);
                return flag;
            }
        }

        /// <summary>
        /// 刷新缓存
        /// </summary>
        /// <returns></returns>
        [HttpPut("refresh")]
        [Api(AllowAnonymous = false, Desc = "刷新字典缓存", Name = "刷新字典缓存", Perm = "dict:refresh")]
        public bool RefreshCache()
        {
            _dictGlobalService.Clear();
            _dictGlobalService.SetGlobalData();
            return true;
        }
    }
}
